package cn.baiweigang.uu.utils;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author 玄罗
 * @date 2021/02/07
 */
public class LogUtil {
    final static Logger logger = LoggerFactory.getLogger(LogUtil.class);

    public static void logInfo(Object obj){
        String str;
        if (obj instanceof String){
            str=obj.toString();
        }else {
            str= JSONObject.toJSONString(obj);
        }
        logInfo(str);
    }
    public static void logInfo(String jsonStringMessage) {
        if (logger.isInfoEnabled()) {
            logger.info(getJsonFormat(jsonStringMessage));
        }
    }

    public static void logDebug(Object obj){
        String str;
        if (obj instanceof String){
            str=obj.toString();
        }else {
            str= JSONObject.toJSONString(obj);
        }
        logDebug(str);
    }

    public static void logDebug(String jsonStringMessage) {
        if (logger.isDebugEnabled()) {
            logger.debug(getJsonFormat(jsonStringMessage));
        }
    }

    public static void logError(Object obj){
        String str;
        if (obj instanceof String){
            str=obj.toString();
        }else {
            str= JSONObject.toJSONString(obj);
        }
        logError(str);
    }

    public static void logError(String jsonStringMessage) {
        if (logger.isErrorEnabled()) {
            logger.error(getJsonFormat(jsonStringMessage));
        }
    }
    public static String getJsonFormat(String s) {
        if (StringUtil.isBlank(s)) {
            return s;
        }
        int level = 0;

        //存放格式化的json字符串
        StringBuffer jsonForMatStr = new StringBuffer();
        for (int index = 0; index < s.length(); index++)//将字符串中的字符逐个按行输出
        {
            //获取s中的每个字符
            char c = s.charAt(index);

            //level大于0并且jsonForMatStr中的最后一个字符为\n,jsonForMatStr加入\t
            if (level > 0 && '\n' == jsonForMatStr.charAt(jsonForMatStr.length() - 1)) {
                jsonForMatStr.append(getLevelStr(level));
            }
            //遇到"{"和"["要增加空格和换行，遇到"}"和"]"要减少空格，以对应，遇到","要换行
            switch (c) {
                case '{':
                case '[':
                    jsonForMatStr.append(c + "\n");
                    level++;
                    break;
                case ',':
                    jsonForMatStr.append(c + "\n");
                    break;
                case '}':
                case ']':
                    jsonForMatStr.append("\n");
                    level--;
                    jsonForMatStr.append(getLevelStr(level));
                    jsonForMatStr.append(c);
                    break;
                default:
                    jsonForMatStr.append(c);
                    break;
            }
        }
        return jsonForMatStr.toString();
    }

    private static String getLevelStr(int level) {
        StringBuffer levelStr = new StringBuffer();
        for (int levelI = 0; levelI < level; levelI++) {
            levelStr.append("\t");
        }
        return levelStr.toString();
    }
}
